package com.mzm.config;

import java.util.List;

import com.mzm.util.persistency.GenericJpaDaoImpl;

/**
 * 
 * @author Daniel Orr
 *
 */
public class ConfigDao extends GenericJpaDaoImpl<Parameter, Integer> {

	public ConfigDao() {
		super(Parameter.class);
	}

	public void createParam(String section, String name, String value){
		Parameter parameter = new Parameter(section, name, value);
		create(parameter);
	}

	@SuppressWarnings("unchecked")
	public Parameter getParam(String section, String name){
		List<Parameter> result = getJpaTemplate().find(
				"SELECT \n" +
				"	p \n" +
				"FROM \n" +
				"	com.mzm.config.Parameter p \n"+
				"WHERE \n" +
				"	p.section = ?1 and p.name = ?2", section, name);
		if(result.isEmpty()){
			return null;
		}
		if(result.size() > 1){
			throw new IllegalStateException(
					"Cannot have two parameters with same name in the same secion");
		}
		return result.get(0);
	}
}
